package com.mj.app.health.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mj.app.health.R;
import com.mj.app.health.base.BaseActivity;
import com.mj.app.health.net.HttpGo;
import com.mj.app.health.net.HttpListener;
import com.mj.app.health.net.response.RegistorResponse;
import com.mj.app.health.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * ========================================
 * *
 * title: RegistorActivity
 * description:
 * *
 * Created by David Hogan on 2018/3/2.
 * ========================================
 */

public class RegistorActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.com_back)
    ImageView comBack;
    @BindView(R.id.com_title)
    TextView comTitle;
    @BindView(R.id.r_username)
    EditText rUsername;
    @BindView(R.id.r_userpwd)
    EditText rUserpwd;
    @BindView(R.id.r_commit_btn)
    Button rCommitBtn;
    @BindView(R.id.r_change_login)
    TextView rChangeLogin;


    private Handler mHandler;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_registor;
    }

    @Override
    protected void init() {

        comTitle.setText("注册");
        setListener();

        mHandler = new Handler() {
            @Override
            public void handleMessage(final Message msg) {
                if(msg.what == 1){

                    ToastUtil.show("注册成功！");
                    back();
                }else{

                    ToastUtil.show("注册失败，请稍后再试或联系管理员");
                }
            }
        };
    }

    private void setListener() {
        comBack.setOnClickListener(this);
        rChangeLogin.setOnClickListener(this);
        rCommitBtn.setOnClickListener(this);
    }

    private void rigistor(){
        // http
        if(judge()){
            final Message message = new Message();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    HttpGo.registor(rUsername.getText().toString().trim(),
                            rUserpwd.getText().toString().trim(),
                            new HttpListener<RegistorResponse>() {
                                @Override
                                public void onSuccess(RegistorResponse registorResponse) {
                                    if(registorResponse.getCode() == 1){
                                        message.what = 1;
                                        mHandler.sendMessage(message);
                                    }else{

                                        message.what = 0;
                                        mHandler.sendMessage(message);
                                    }

                                }
                                @Override
                                public void onError(String exception) {

                                    message.what = 0;
                                    mHandler.sendMessage(message);
                                }
                            });
                }
            }).start();
        }
    }

    private boolean judge() {

        // 校验，如果是电话注册后续可自行加入电话正则校验

        if(TextUtils.isEmpty(rUsername.getText()) || "".equals(rUsername.getText().toString().trim())){
            ToastUtil.show("用户名不能为空");
            return false;
        }

        if(TextUtils.isEmpty(rUserpwd.getText()) || "".equals(rUserpwd.getText().toString().trim())){
            ToastUtil.show("密码不能为空");
            return false;
        }

        if(rUserpwd.getText().length()<3 || rUserpwd.getText().length()>10){
            ToastUtil.show("请输入3-10位数字或英文");
            return false;
        }

        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.com_back:
                back();
                break;
            case R.id.r_commit_btn: // zhuce
                rigistor();
                break;
            case R.id.r_change_login: // fanhui denglu
                back();
                break;
        }
    }
}
